#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=2*1e5+50;
int pos[N];
int main(void){
    int n;
    char o;
    int idx;
    int now_l=0;
    int now_r=0;
    int tot=1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        getchar();
        scanf("%c%d",&o,&idx);
        if(i==1){
            pos[idx]=0;
            now_l--;
            now_r++;
        }
        else{
            if(o=='L'){
                pos[idx]=now_l--;
            }
            else if(o=='R'){
                pos[idx]=now_r++;
            }
            else{
                printf("%d\n",min(abs(pos[idx]-now_l),abs(pos[idx]-now_r))-1);
            }
        }
    }
    return 0;
}